package com.example.hissys.mapper;

import com.example.hissys.entity.Appointment;
import com.example.hissys.dto.AppointmentDetailDTO;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface AppointmentMapper {
    /**
     * 创建预约记录
     */
    @Insert("INSERT INTO appointments (user_id, doctor_id, appointment_date, appointment_time, status, payment_status) " +
            "VALUES (#{userId}, #{doctorId}, #{appointmentDate}, #{appointmentTime}, 'pending', 'unpaid')")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int create(Appointment appointment);

    /**
     * 检查用户在指定日期和时间段是否已有有效预约
     * 只检查未取消且未支付的预约
     */
    @Select("SELECT COUNT(*) FROM appointments " +
            "WHERE user_id = #{userId} " +
            "AND doctor_id = #{doctorId} " +
            "AND appointment_date = #{date} " +
            "AND appointment_time = #{time} " +
            "AND status != 'cancelled' " +
            "AND payment_status = 'unpaid'")
    int checkExistingAppointment(
        @Param("userId") Integer userId,
        @Param("doctorId") Integer doctorId,
        @Param("date") String date,
        @Param("time") String time
    );

    /**
     * 获取用户预约列表
     */
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "a.doctor_id, " +
            "a.appointment_date, " +
            "a.appointment_time, " +
            "a.status, " +
            "a.payment_status, " +
            "a.created_at, " +
            "d.name as doctorName, " +
            "dep.name as departmentName, " +
            "h.name as hospitalName " +
            "FROM appointments a " +
            "LEFT JOIN doctors d ON a.doctor_id = d.id " +
            "LEFT JOIN departments dep ON d.department_id = dep.id " +
            "LEFT JOIN hospitals h ON dep.hospital_id = h.id " +
            "WHERE a.user_id = #{userId} " +
            "ORDER BY a.created_at DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "doctorId", column = "doctor_id"),
        @Result(property = "appointmentDate", column = "appointment_date"),
        @Result(property = "appointmentTime", column = "appointment_time"),
        @Result(property = "status", column = "status"),
        @Result(property = "paymentStatus", column = "payment_status"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "doctorName", column = "doctorName"),
        @Result(property = "departmentName", column = "departmentName"),
        @Result(property = "hospitalName", column = "hospitalName")
    })
    List<AppointmentDetailDTO> findByUserId(@Param("userId") Integer userId);

    /**
     * 取消预约（删除预约记录）
     */
    @Delete("DELETE FROM appointments WHERE id = #{id}")
    int cancelAppointment(@Param("id") Integer id);

    /**
     * 支付预约
     */
    @Update("UPDATE appointments " +
            "SET payment_status = 'paid' " +
            "WHERE id = #{id} " +
            "AND payment_status = 'unpaid'")
    int payAppointment(@Param("id") Integer id);

    /**
     * 检查预约是否属于该用户
     */
    @Select("SELECT COUNT(*) FROM appointments " +
            "WHERE id = #{id} AND user_id = #{userId}")
    int checkAppointmentOwnership(@Param("id") Integer id, @Param("userId") Integer userId);

    /**
     * 根据ID查询预约详情
     */
    @Select("SELECT " +
            "a.id, " +
            "a.user_id, " +
            "a.doctor_id, " +
            "a.appointment_date, " +
            "a.appointment_time, " +
            "a.status, " +
            "a.payment_status, " +
            "a.created_at, " +
            "d.name as doctorName, " +
            "dep.name as departmentName, " +
            "h.name as hospitalName " +
            "FROM appointments a " +
            "LEFT JOIN doctors d ON a.doctor_id = d.id " +
            "LEFT JOIN departments dep ON d.department_id = dep.id " +
            "LEFT JOIN hospitals h ON dep.hospital_id = h.id " +
            "WHERE a.id = #{id}")
    AppointmentDetailDTO findById(@Param("id") Integer id);
} 